<?php
/**
 * Created by PhpStorm.
 * User: Sinri
 * Date: 2017/9/23
 * Time: 13:52
 */

namespace sinri\wenli;


use DOMDocument;

class DutyForWenli extends PageReader
{
    public function version()
    {
        return "wenli";
    }

    public function duty()
    {
        $chapters = [
            //舊約
            "創世記-（創）-50章",
            "出埃及記-（出）-40章",
            "利未記-（利）-27章",
            "民數記-（民）-36章",
            "申命記-（申）-34章",
            "約書亞記-（書）-24章",
            "士師記-（士）-21章",
            "路得記-（得）-4章",
            "撒母耳記上-（撒上）-31章",
            "撒母耳記下-（撒下）-24章",
            "列王紀上-（王上）-22章",
            "列王紀下-（王下）-25章",
            "歷代志上-（代上）-29章",
            "歷代志下-（代下）-36章",
            "以斯拉記-（拉）-10章",
            "尼希米記-（尼）-13章",
            "以斯帖記-（斯）-10章",
            "約伯記-（伯）-42章",
            "詩篇-（詩）-150篇",
            "箴言-（箴）-31章",
            "傳道書-（傳）-12章",
            "雅歌-（歌）-8章",
            "以賽亞書-（賽）-66章",
            "耶利米書-（耶）-52章",
            "耶利米哀歌-（哀）-5章",
            "以西結書-（結）-48章",
            "但以理書-（但）-12章",
            "何西阿書-（何）-14章",
            "約珥書-（珥）-3章",
            "阿摩司書-（摩）-9章",
            "俄巴底亞書-（俄）-1章",
            "約拿書-（拿）-4章",
            "彌迦書-（彌）-7章",
            "那鴻書-（鴻）-3章",
            "哈巴谷書-（哈）-3章",
            "西番雅書-（番）-3章",
            "哈該書-（該）-2章",
            "撒迦利亞書-（亞）-14章",
            "瑪拉基書-（瑪）-4章",
            //新約
            "馬太福音-（太）-28章",
            "馬可福音-（可）-16章",
            "路加福音-（路）-24章",
            "約翰福音-（約）-21章",
            "使徒行傳-（徒）-28章",
            "羅馬人書-（羅）-16章",
            "哥林多前書-（林前）-16章",
            "哥林多後書-（林後）-13章",
            "加拉太書-（加）-6章",
            "以弗所書-（弗）-6章",
            "腓立比書-（腓）-4章",
            "歌羅西書-（西）-4章",
            "帖撒羅尼迦前書-（帖前）-5章",
            "帖撒羅尼迦後書-（帖後）-3章",
            "提摩太前書-（提前）-6章",
            "提摩太後書-（提後）-4章",
            "提多書-（多）-3章",
            "腓利門書-（門）-1章",
            "希伯來書-（來）-13章",
            "雅各書-（雅）-5章",
            "彼得前書-（彼前）-5章",
            "彼得後書-（彼後）-3章",
            "約翰一書-（約壹）-5章",
            "約翰二書-（約貳）-1章",
            "約翰三書-（約叁）-1章",
            "猶大書-（猶）-1章",
            "啟示錄-（啟）-22章",
        ];

        for ($i = 0; $i < count($chapters); $i++) {
            $parts = explode("-", $chapters[$i]);
            $chapter_id = $i + 1;
            if ($chapter_id < 10) $chapter_id = "0" . $chapter_id;
            $chapter_name = $parts[0];
            $chapter_count = $parts[2];
            $this->dutyForBook($chapter_id, $chapter_name, $chapter_count);
        }
    }

    protected function getBookURL($book_id, $book_name = null, $chapter_id = null)
    {
        $url_base = "https://zh.wikisource.org/wiki/%E8%81%96%E7%B6%93_(%E6%96%87%E7%90%86%E5%92%8C%E5%90%88)/";
        $url = $url_base . urlencode($book_name);
        return $url;
    }

    protected function html2txt($html, $chapter = null)
    {
        $doc = new DOMDocument();
        @$doc->loadHTML($html);
        $element1 = $doc->getElementById("mw-content-text");
        $element2 = $element1->firstChild;

        $chapters = [];
        for ($i = 0; $i < $element2->childNodes->length; $i++) {
            $element3 = $element2->childNodes->item($i);
            if (!isset($element3->tagName)) {
                continue;
            }
            if ($element3->tagName == "p") {
                $span = $element3->firstChild;
                if (preg_match('/^(\d+):(\d+)$/', $span->getAttribute("id"), $matches)) {
                    $span_chapter = $matches[1];
                    $span_verse = $matches[2];

                    if (!isset($chapters[$span_chapter])) {
                        $chapters[$span_chapter] = "";
                    }

                    $element3->removeChild($element3->firstChild);
                    $word = $element3->nodeValue;
                    $chapters[$span_chapter] .= "{$span_chapter}:{$span_verse} " . $word . PHP_EOL;
                }
            }
        }

        return $chapters;
    }
}